/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.blitscreen.camera
{
	import flash.display.Sprite;
	
	import com.bourre.log.PixlibDebug;	

	/**
	 * A basic implementation of the <code>CameraListener</code>
	 * interface. The <code>CameraLayer</code> object is a simple
	 * <code>Sprite</code> which scroll according to the camera
	 * screen and its own <code>parallax</code> property.
	 * <p>
	 * Parallax is a multiplication factor for the <code>x</code>
	 * and <code>y</code> coordinates of the layer. For example,
	 * if a layer have a <code>parallax</code> value of <code>0.5</code>,
	 * when the camera's screen x position is <code>100</code> the 
	 * resulting x position of the layer will be <code>-50</code>.
	 * </p><p>
	 * Scrolling is performed like this :
	 * The position of a layer is always the camera screen coordinates
	 * inverse. As camera screen is a virtual screen, concret layers
	 * will move backside in comparison to the camera. 
	 * </p>
	 * @author	CédricNéhémie
	 * @see		Camera
	 * @see		CameraListener
	 */
	public class CameraLayer extends Sprite implements CameraListener
	{
		/**
		 * Parallax value for this <code>CameraLayer</code>, there is
		 * no limitation for this value. 
		 * <p>
		 * If the value is <code>NaN</code> the position will never change.
		 * </p>
		 */
		public var parallax : Number;
		
		/**
		 * Creates a new <code>CameraLayer</code> object with the
		 * specified <code>parallax</code> value.
		 * 
		 * @param parallax (optional) <code>Number</code> value for
		 * 				   this <code>CameraLayer</code> object
		 */
		public function CameraLayer ( parallax : Number = 1 )
		{
			this.parallax = parallax;
		}
		
		/**
		 * @inheritDoc
		 */
		public function onCameraChange( e : CameraEvent ) : void
		{
			var zoom : Number = e.getZoom();
			
			x = -e.getScreen().x * zoom * parallax;
			y = -e.getScreen().y * zoom * parallax;
			scaleX = zoom;			scaleY = zoom;
		}
	}
}